import os
from utils.file_utils import get_detection_data_path, get_rootheal_data_path, read_file_content

# 获取异常检测的文件列表
def get_detection_files_list():
    detection_data_path = get_detection_data_path()
    # print("777777777777777777777777")
    # print(detection_data_path)
    try:
        files = os.listdir(detection_data_path)
        file_list = [
            {
                'name': file,
                'url': f'/detection-data/{file}'
            }
            for file in files if os.path.isfile(os.path.join(detection_data_path, file))
        ]
        return file_list
    except Exception as e:
        return {'error': f'Failed to read directory: {str(e)}'}

# 获取rootheal_data目录下的文件列表
def get_rootheal_files_list():
    rootheal_data_path = get_rootheal_data_path()
    try:
        files = os.listdir(rootheal_data_path)
        file_list = [
            {
                'name': file,
                'url': f'/rootheal-data/{file}'
            }
            for file in files if os.path.isfile(os.path.join(rootheal_data_path, file))
        ]
        return file_list
    except Exception as e:
        return {'error': f'Failed to read directory: {str(e)}'}

# 获取特定文件内容
def get_detection_file_content(filename):
    try:
        file_path = os.path.join(get_detection_data_path(), filename)
        
        # 检查文件是否存在
        if not os.path.exists(file_path):
            return {'error': 'File not found'}
        
        # 读取文件内容
        content = read_file_content(file_path)
        return {
            'name': filename,
            'content': content
        }
    except Exception as e:
        return {'error': f'Failed to read file: {str(e)}'}

# 获取rootheal_data目录下特定文件内容
def get_rootheal_file_content(filename):
    try:
        file_path = os.path.join(get_rootheal_data_path(), filename)
        
        # 检查文件是否存在
        if not os.path.exists(file_path):
            return {'error': 'File not found'}
        
        # 读取文件内容
        content = read_file_content(file_path)
        return {
            'name': filename,
            'content': content
        }
    except Exception as e:
        return {'error': f'Failed to read file: {str(e)}'}
